package com.wanggsx.library.util.fun.timer;

import android.os.Handler;
import android.os.Message;

import java.util.Timer;
import java.util.TimerTask;

public final class TimerManager {
    public static Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                TimerCallback timerCallback = (TimerCallback) msg.obj;
                timerCallback.timerCallback(msg.arg1);
            } else {
                Timer timer = (Timer) msg.obj;
                timer.cancel();
            }
        }
    };

    /**
     * 开启计时器执行任务
     *
     * @param delay         多少毫秒后开始执行任务
     * @param isRepeat      是否需要重复执行任务
     * @param times         重复执行次数，默认无限次(0或-1均代表无限次)
     * @param period        如果需要重复执行，则间隔多少毫秒执行一次任务
     * @param timerCallback 执行任务需触发的回调
     * @return
     */
    public static Timer startTimer(long delay, final boolean isRepeat,
                                   final int times, long period, final TimerCallback timerCallback) {
        final Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            int leftTimes = times;

            @Override
            public void run() {
                if (isRepeat) {
                    if ((times >= 1 && leftTimes-- >= 0) || times <= 0) {
                        Message msg = new Message();
                        msg.obj = timerCallback;
                        msg.arg1 = times - leftTimes;
                        msg.what = 1;
                        mHandler.sendMessage(msg);
                    } else {
                        Message msg = new Message();
                        msg.obj = timerCallback;
                        msg.what = 2;
                        mHandler.sendMessage(msg);
                    }
                } else {
                    Message msg = new Message();
                    msg.obj = timerCallback;
                    msg.arg1 = 1;
                    msg.what = 1;
                    mHandler.sendMessage(msg);
                }
            }
        };
        if (isRepeat) {
            timer.schedule(task, delay, period);
        } else {
            timer.schedule(task, delay);
        }
        return timer;
    }

    public static void cancelTimer(Timer timer) {
        if (timer != null) {
            timer.cancel();
        }
    }
}
